package cn.iocoder.yudao.framework.beanconfig;


import cn.iocoder.yudao.framework.cache.NettyChannelCacheService;
import cn.iocoder.yudao.framework.cache.NettyWsUserChannelCacheService;
import cn.iocoder.yudao.framework.common.NettyAsyncTaskService;
import cn.iocoder.yudao.framework.common.NettyFrameworkConfig;
import cn.iocoder.yudao.framework.handler.NettyWsRequestDispatcherHandler;
import cn.iocoder.yudao.framework.prometheus.ws.WebSocketConnectionMetrics;
import cn.iocoder.yudao.framework.prometheus.ws.WebSocketMessageMetrics;
import cn.iocoder.yudao.framework.server.websocket.security.WebTokenAuthorizationHandler;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author 王广帅
 * @date 2021年01月26日 8:09 下午
 */
@Configuration
@EnableConfigurationProperties(NettyFrameworkConfig.class)
public class AutoNettyServerConfiguration {

    @Bean
    public WebSocketConnectionMetrics webSocketConnectionMetrics() {
        return new WebSocketConnectionMetrics();
    }

    @Bean
    public WebSocketMessageMetrics webSocketMessageMetrics() {
        return new WebSocketMessageMetrics();
    }

    @Bean
    public NettyAsyncTaskService gameIOTaskService() {
        return new NettyAsyncTaskService();
    }

    @Bean
    public NettyWsUserChannelCacheService gameChannelService() {
        return new NettyWsUserChannelCacheService();
    }

    @Bean
    public NettyChannelCacheService nettyChannelService() {
        NettyChannelCacheService nettyChannelCacheService = new NettyChannelCacheService();
        //nettyChannelCacheService.setWebSocketConnectionMetrics(webSocketConnectionMetrics);
        return nettyChannelCacheService;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public NettyWsRequestDispatcherHandler gameRequestDispatcherHandler() {
        NettyWsRequestDispatcherHandler nettyWsRequestDispatcherHandler = new NettyWsRequestDispatcherHandler();
        //nettyWsRequestDispatcherHandler.setWebSocketMessageMetrics(webSocketMessageMetrics);
        return nettyWsRequestDispatcherHandler;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public WebTokenAuthorizationHandler webTokenAuthorizationHandler() {
        WebTokenAuthorizationHandler webTokenAuthorizationHandler = new WebTokenAuthorizationHandler();
        //webTokenAuthorizationHandler.setWebSocketConnectionMetrics(webSocketConnectionMetrics);
        return webTokenAuthorizationHandler;
    }

}
